package com.cuitrip.business.home.travel.ui;

import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.cuitrip.business.home.travel.model.Destination;
import com.cuitrip.business.home.travel.model.FilterExp;
import com.cuitrip.business.home.travel.model.FilterInfo;
import com.cuitrip.business.home.travel.model.SelectInfo;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.component.popupmenu.PopupMenu;
import com.cuitrip.component.popupmenu.PopupMenuItem;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ak on 4/7/16.
 */
public class ConditionViewHolder {

    public static final int DEFAULT_MODE = 0;
    public static final int DESTINATION_MODE = 0x01;
    public static final int SORT_MODE = 0x02;
    public static final int FILTER_MODE = 0x03;
    static final ButterKnife.Setter<TextView, Boolean> SELECTED = new ButterKnife.Setter<TextView, Boolean>() {
        @Override
        public void set(TextView view, Boolean value, int index) {
            if (value) {
                view.setTextColor(ResourceUtil.getThemeColor());
            } else {
                view.setTextColor(ResourceUtil.getColor(R.color.qiaomai_7c706e));
            }
        }
    };
    View inflatedView;
    @Bind(R.id.sort_text_item)
    TextView sortText;
    @Bind(R.id.sort_arrow_item)
    IconTextView sortArrowIcon;
    @Bind(R.id.destination_arrow_item)
    IconTextView destinationArrowIcon;
    @Bind(R.id.filter_arrow_item)
    IconTextView filterArrowIcon;
    @Bind({R.id.sort_text_item, R.id.sort_arrow_item})
    List<TextView> sortTextCollection;
    @Bind({R.id.destination_text_item, R.id.destination_arrow_item})
    List<TextView> destinationTextCollection;
    @Bind({R.id.filter_text_item, R.id.filter_arrow_item})
    List<TextView> filterTextCollection;
    @Bind(R.id.sort_layout)
    View sortLayout;
    @Bind(R.id.filter_layout)
    View filterLayout;
    @Bind(R.id.destination_stub)
    ViewStub mDestinationStub;
    View mDestinationView;
    @Bind(R.id.filter_stub)
    ViewStub mFilterStub;
    View mFilterView;
    private int mMode = DEFAULT_MODE;
    private TravelTripListView travelTripListView;
    private SelectInfo mSelectInfo;
    private OnSortChangedListener mOnSortChangedListener;
    private PopupFilterViewHolder mFilterViewHolder;
    private PopupFilterViewHolder mDestinationViewHolder;

    public ConditionViewHolder(View view, TravelTripListView travelTripListView, SelectInfo selectInfo) {
        this.inflatedView = view;
        this.mSelectInfo = selectInfo;
        this.travelTripListView = travelTripListView;
        ButterKnife.bind(this, inflatedView);
    }

    private void setMode(int mode) {
        this.mMode = mode;
    }

    public void setOnSortChangedListener(OnSortChangedListener onSortChangedListener) {
        this.mOnSortChangedListener = onSortChangedListener;
    }

    @OnClick(R.id.sort_layout)
    void showSortList() {
        if (mMode == FILTER_MODE || mMode == DESTINATION_MODE) {
            leaveMode(mMode);
        }
        final List<PopupMenuItem> popupMenuItemList = new ArrayList<PopupMenuItem>();
        final FilterExp filterExp = mSelectInfo.getFilterExp();
        for (int i = 0; i < filterExp.getSortListSize(); i++) {
            PopupMenuItem popupMenuItem = new PopupMenuItem(filterExp.getSort(i).getDesc());
            popupMenuItemList.add(popupMenuItem);
            if (i == filterExp.getSelectSortPosition()) {
                popupMenuItem.setSelected(true);
            }
        }

        PopupMenu popupMenu = PopupMenu.getInstance(travelTripListView.getContext(), popupMenuItemList, new PopupMenu.PopupMenuCallback() {
            @Override
            public boolean onPopupMenuItemClick(int position, PopupMenuItem popupMenuItem) {
                filterExp.setSelectSortPosition(position);
                if (mOnSortChangedListener != null) {
                    mOnSortChangedListener.onChanged(filterExp.getSelectSort());
                }
                return false;
            }

            @Override
            public void onPopupMenuStateChanged(boolean isShow) {
                if (isShow) {
                    sortArrowIcon.animate().rotation(180);
                    travelTripListView.showBlurBackground();
                    ButterKnife.apply(sortTextCollection, SELECTED, true);
                    setMode(SORT_MODE);
                } else {
                    sortArrowIcon.animate().rotation(0);
                    travelTripListView.hideBlurBackground();
                    ButterKnife.apply(sortTextCollection, SELECTED, false);
                    setMode(DEFAULT_MODE);
                }
            }
        });

        if (popupMenu != null) {
            popupMenu.show(sortLayout);
        }
    }

    @OnClick(R.id.destination_layout)
    void showDestinationList() {
        if (mMode == FILTER_MODE) {
            leaveMode(mMode);
        }
        if (mDestinationView == null) {
            mDestinationView = mDestinationStub.inflate();
            mDestinationView.setVisibility(View.VISIBLE);
        }
        final FilterExp filterExp = mSelectInfo.getFilterExp();
        if (mDestinationViewHolder == null) {
            mDestinationViewHolder = new PopupFilterViewHolder<Destination, Destination.Value>(travelTripListView.getContext(), PopupFilterViewHolder.DESTINATION_TYPE, filterExp.getDestExp(), mDestinationView, new PopupFilterViewHolder.PopupMenuCallback() {
                @Override
                public boolean onPopupMenuItemClick(int position, Object item) {
                    return false;
                }

                @Override
                public void onPopupMenuStateChanged(boolean isShow) {
                    if (isShow) {
                        destinationArrowIcon.animate().rotation(180);
                        travelTripListView.showRightButton();
                        ButterKnife.apply(destinationTextCollection, SELECTED, true);
                        setMode(DESTINATION_MODE);
                    } else {
                        destinationArrowIcon.animate().rotation(0);
                        travelTripListView.hideRightButton();
                        ButterKnife.apply(destinationTextCollection, SELECTED, false);
                        setMode(DEFAULT_MODE);
                    }

                }
            });
            mDestinationViewHolder.show();
        } else {
            if (mDestinationViewHolder.isShow()) {
                mDestinationViewHolder.hide();
            } else {
                mDestinationViewHolder.show();
            }
        }
    }

    @OnClick(R.id.filter_layout)
    void showFilterList() {
        if (mMode == DESTINATION_MODE) {
            leaveMode(mMode);
        }
        if (mFilterView == null) {
            mFilterView = mFilterStub.inflate();
            mFilterStub.setVisibility(View.VISIBLE);
        }
        if (mFilterViewHolder == null) {
            final FilterExp filterExp = mSelectInfo.getFilterExp();
            ButterKnife.apply(destinationTextCollection, SELECTED, false);
            mFilterViewHolder = new PopupFilterViewHolder<FilterInfo, FilterInfo.ValueInfo>(travelTripListView.getContext(), PopupFilterViewHolder.CATEGORY_TYPE, filterExp.getFilterExp(), mFilterView, new PopupFilterViewHolder.PopupMenuCallback() {
                @Override
                public boolean onPopupMenuItemClick(int position, Object item) {
                    return false;
                }

                @Override
                public void onPopupMenuStateChanged(boolean isShow) {
                    if (isShow) {
                        filterArrowIcon.animate().rotation(180);
                        travelTripListView.showRightButton();
                        ButterKnife.apply(filterTextCollection, SELECTED, true);
                        setMode(FILTER_MODE);
                    } else {
                        filterArrowIcon.animate().rotation(0);
                        travelTripListView.hideRightButton();
                        ButterKnife.apply(filterTextCollection, SELECTED, false);
                        setMode(DEFAULT_MODE);
                    }

                }
            });
            mFilterViewHolder.show();
        } else {
            if (mFilterViewHolder.isShow()) {
                mFilterViewHolder.hide();
            } else {
                mFilterViewHolder.show();
            }
        }
    }


    private void leaveMode(int mode) {
        if (mode == FILTER_MODE) {
            mFilterViewHolder.hide();
        } else if (mode == DESTINATION_MODE) {
            mDestinationViewHolder.hide();
        }
    }

    public void leaveCurrentMode() {
        leaveMode(mMode);
    }


    public void destroy() {
        mOnSortChangedListener = null;
        travelTripListView = null;
        ButterKnife.unbind(this);

    }

    public boolean backPress() {
        if (mMode == FILTER_MODE || mMode == DESTINATION_MODE) {
            leaveMode(mMode);
            return true;
        }
        return false;
    }


}
